<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product_model extends MY_Model {
    
    protected $TB_PRODUCT = 'dc_product';
    protected $PRODUCT_COLS = array(
        'product_id',
        'product_order',
        'product_required_votes',    
        'product_high_discount',
        'product_low_discount',
        'product_full_price_link',
        'product_high_discount_link',
        'product_low_discount_link',
        'product_high_discount_picture',
        'product_low_discount_picture',
        'product_pre_vote_picture',
        'product_sharing_picture',
        'product_vote_count',
        'product_share_count',
        'product_name',
        'product_create_time'
    );
    
    public function __construct() {
        parent::__construct();
    }

    public function add_product($product_order, $product_required_votes,
		$product_high_discount, $product_low_discount, $product_full_price_link,
		$product_high_discount_link, $product_low_discount_link,
		$product_high_discount_picture, $product_low_discount_picture,
		$product_sharing_picture, $product_vote_count, $product_share_count,
		$product_create_time) {
        $product_create_time = $product_create_time or date('Y-m-d H:i:s', time());
        $query = $this->db->query("INSERT INTO $this->TB_PRODUCT 
            (`product_order`, `product_required_votes`, `product_high_discount`,
			`product_low_discount`, `product_full_price_link`,
			`product_high_discount_link`, `product_low_discount_link`,
			`product_high_discount_picture`, `product_low_discount_picture`,
			`product_sharing_picture`, `product_vote_count`, `product_share_count`,
			`product_create_time`) VALUES
            ('$product_order', '$product_required_votes', '$product_high_discount',
			'$product_low_discount', '$product_full_price_link', '$product_high_discount_link',
			'$product_low_discount_link', '$product_high_discount_picture',
			'$product_low_discount_picture', '$product_sharing_picture',
			'$product_vote_count', '$product_share_count', '$product_create_time')");
        $product_id = $query ? $this->db->insert_id() : false;
        return $product_id;
    }
    
    public function increase_product_vote_count($product_id) {
        return $this->db->query("UPDATE $this->TB_PRODUCT 
            SET `product_vote_count` = product_vote_count + 1
            WHERE `product_id` = $product_id");
    }
    
    public function increase_product_share_count($product_id) {
        return $this->db->query("UPDATE $this->TB_PRODUCT 
            SET `product_share_count` = product_share_count + 1 
            WHERE `product_id` = $product_id");
    }
    
    public function del_product($product_id) {
        return $this->db->query("DELETE FROM $this->TB_PRODUCT
            WHERE `product_id` = $product_id LIMIT 1");
    }
    
    public function set_product($product_id, $sets) {
        $sets_sql = $this->_sets_to_str($sets, $this->PRODUCT_COLS);
        return $this->db->query("UPDATE $this->TB_PRODUCT SET $sets_sql 
            WHERE `product_id` = $product_id");
    }
    
    public function set_product_orders($sql_set, $sql_where) {
        return $this->db->query("UPDATE `dc_product` set `product_order` = ".$sql_set." where `product_id` = ".$sql_where);
    }
    
    public function get_product($product_id, $cols=null) {
        $cols_str = $cols ? join(',', $cols) : join(',', $this->PRODUCT_COLS);
        return $this->db->query("SELECT $cols_str FROM $this->TB_PRODUCT
            WHERE `product_id` = $product_id LIMIT 1")->row_array();
    }
    
    public function get_product_sharing_picture($pid) {
        $query = $this->db->query("SELECT `product_sharing_picture` 
            FROM $this->TB_PRODUCT WHERE `product_id` = $pid LIMIT 1");
        return $query->num_rows ? current($query->row()) : null;
    }
    
    public function get_all_products($cols=null) {
        $cols_str = $cols ? join(',', $cols) : join(',', $this->PRODUCT_COLS);
        return $this->db->query("SELECT $cols_str FROM $this->TB_PRODUCT ORDER BY product_order")
            ->result(0);
    }
    
    public function set_product_high_discount_picture($product_id, $picture) {
        return $this->db->query("UPDATE $this->TB_PRODUCT 
            SET `product_high_discount_picture` = '$picture'
            WHERE `product_id` = '$product_id'");
    }
    
    public function set_product_low_discount_picture($product_id, $picture) {
        return $this->db->query("UPDATE $this->TB_PRODUCT 
            SET `product_low_discount_picture` = '$picture'
            WHERE `product_id` = '$product_id'");
    }
    
    public function set_product_sharing_picture($product_id, $picture) {
        return $this->db->query("UPDATE $this->TB_PRODUCT 
            SET `product_sharing_picture` = '$picture' 
            WHERE `product_id` = $product_id");
    }
    
    public function set_product_pre_vote_picture($product_id, $picture) {
        return $this->db->query("UPDATE $this->TB_PRODUCT 
            SET `product_pre_vote_picture` = '$picture' 
            WHERE `product_id` = $product_id");
    }
    
    public function set_product_vote_count($product_id, $vote_count) {
        return $this->db->query("UPDATE $this->TB_PRODUCT 
            SET `product_vote_count` = '$vote_count' 
            WHERE `product_id` = $product_id");
    }
    
    public function set_product_share_count($product_id, $share_count) {
        return $this->db->query("UPDATE $this->TB_PRODUCT
            SET `product_share_count` = '$share_count'
            WHERE `product_id` = $product_id");
    }
}